import axios from 'axios'
import Message from '@/components/Message/index'
import { checkStatus } from "./status";

//创建axios实例
const service = axios.create({
    timeout: 600000
});


//请求拦截
service.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('Authorization-Token')
        if (token) {
            config.headers.Authorization = token;
        }
        return config

    },
    (error) => Message.error(error)
)


//响应拦截
service.interceptors.response.use(
    (response) => {
   
        const { data } = response;
        if(data.data && data.data == '未登录'){
            window.location.href = "/login.html";
            return;
        }
        if(data.code){
            if (data.code <= 200 || data.code > 300) {
                return data;
            } else {
                // 处理业务错误。
                const msg = checkStatus(data.code);
                Message.error(msg);
                return response;
            }
        }else if(data.status){
            if(data.status === "ok"){
                return data;
            }else{
                return data;
            }
        }
       
    },
    (error) => {
       const {status} = error.response
       if(status === 500) return Message.error(error.message)
        // 捕获到抛出的错误
        Promise.reject(error);
    }

)
export default service
